-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sherlock and the Valid String.c
56 lines (54 loc) · 1.31 KB
/
Sherlock and the Valid String.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
char* isValid(char* s)
{
int i=0,j,flag=1,prev=0,later=0,first_time_flag=1,isValid=1,counter1=0,counter2=0;
char alphaFreq['z'-'a'+1]={0};
while (s[i]!='\0')
{
alphaFreq[ (s[i]-'a') ]++;
i++;
}
for(j=0;(j<('z'-'a'+1))&&(isValid==1);j++)
{
if ( alphaFreq[j]>0 )
{
if (first_time_flag==1)
{
first_time_flag=0;
prev=j;
}
if (alphaFreq[prev]!=alphaFreq[j])
{
if (alphaFreq[j]==1)
{
counter1++;//<<<<<<<<<<<<<<<<<<<<<<<<
}
else if ( (alphaFreq[prev]+1)==alphaFreq[j] )
{
counter2++;//<<<<<<<<<<<<<<<<<<<<<<<<
}
else
{
isValid=0;
}
if ( (counter1>1)||(counter2>1)||(counter1==1&&counter2==1) )
{
isValid=0;
}
}
else if (alphaFreq[prev]==alphaFreq[j])
{
prev=j;
}
}
}
if(isValid==1)
{
return "YES";
//printf("YES");
}
else
{
return "NO";
//printf("NO");
}
}